package com.qf.zyh.common;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component//设置一个空的方法，然后写切点表达式，方法名就座位表达式的id使用
public class MyAdvice {
    @Pointcut("execution(* com.qf.zyh.service..*.*(..))")
    public void pointCut(){

    }
    //前置注解
    @Before(value = "pointCut()")
    public void before(JoinPoint jp){
        System.out.println("目标名"+jp.getTarget().getClass().getSimpleName());
        System.out.println(Arrays.toString(jp.getArgs()));
    }
    //后置注解
    @AfterReturning(pointcut = "pointCut()",returning = "o")
    public void after(JoinPoint jp,Object o){
        System.out.println("猴子");
    }
    @Around(value = "pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前置");
        Object proceed = pjp.proceed();
        System.out.println("环绕后置");
        return proceed;
        //中间放行之后需要获取到然后返回出去，不返回无法显示中间操作，也无法提现环绕操作
    }
    //异常注解，需要设置异常
    @AfterThrowing(pointcut = "pointCut()",throwing = "ex")
    public void chucuola(JoinPoint jp,Exception ex){
        System.out.println("异常 增强");
        System.out.println("名字"+jp.getTarget().getClass().getSimpleName());
        System.out.println("*****"+ Arrays.toString(jp.getArgs()));
        System.out.println("*****"+System.currentTimeMillis());
    }
}
